#include <iostream>
#include <memory>
#include <string>

class Fruit
{
public:
    virtual void Name() = 0;
};
class Apple:public Fruit
{
public:
    void Name() override
    {
        std::cout << "我是一个苹果!" << std::endl;
    }
};
class Banana:public Fruit
{
public:
    void Name() override
    {
        std::cout << "我是一个香蕉!" << std::endl;
    }
};

class Animal
{
public:
    virtual void Name() = 0;
};
class Tiger : public Animal
{
public:
    void Name() override
    {
        std::cout << "我是一只老虎!" << std::endl;
    }
};
class Cat : public Animal
{
public:
    void Name() override
    {
        std::cout << "我是一只病猫！" << std::endl;
    }
};

class Factory 
{
public:
    virtual std::shared_ptr<Fruit> GetFruit(const std::string& name) = 0;
    virtual std::shared_ptr<Animal> GetAnimal(const std::string& name) = 0;

};
class FruitFactory : public Factory
{
public:
    std::shared_ptr<Fruit> GetFruit(const std::string& name) override
    {
        if(name == "苹果")
        {
            return std::make_shared<Apple>();
        }
        else
        {
            return std::make_shared<Banana>();
        }
    }
    std::shared_ptr<Animal> GetAnimal(const std::string& name) 
    {
        return std::shared_ptr<Animal>();
    }
   
};
class AnimalFactory : public Factory
{
public:
    std::shared_ptr<Fruit> GetFruit(const std::string& name) override
    {
        return std::shared_ptr<Fruit>();
       
    }
    std::shared_ptr<Animal> GetAnimal(const std::string& name) 
    {
        if(name == "老虎")
        {
            return std::make_shared<Tiger>();
        }
        else
        {
            return std::make_shared<Cat>();
        }
    }
};

class FactoryProducer
{
public:
    static std::shared_ptr<Factory> create(const std::string& name)
    {
        if(name == "水果")
        {
            return std::make_shared<FruitFactory>();
        }
        else if(name == "动物")
        {
            return std::make_shared<AnimalFactory>();
        }
        return nullptr;
    }
    
};


// class FruitFactory
// {
// public:
//     virtual std::shared_ptr<Fruit> create() = 0;
// };
// class AppleFactory : public FruitFactory
// {
// public:
//     std::shared_ptr<Fruit> create() override
//     {
//         return std::make_shared<Apple>();
//     }
// };
// class BananaFactory : public FruitFactory
// {
// public:
//     std::shared_ptr<Fruit> create() override
//     {
//         return std::make_shared<Banana>();
//     }
// };
int main()
{
    // std::shared_ptr<Fruit> apple = FruitFactory::create("苹果");
    // apple->Name();
    // std::shared_ptr<Fruit> banana = FruitFactory::create("香蕉");
    // banana->Name();

    // std::shared_ptr<FruitFactory> ff(new AppleFactory());
    // std::shared_ptr<Fruit> apple = ff->create();
    // apple->Name();

    // ff.reset(new BananaFactory());
    // std::shared_ptr<Fruit> banana = ff->create();
    // banana->Name();
    std::shared_ptr<Factory> ff = FactoryProducer::create("水果");
    std::shared_ptr<Fruit> fruit = ff->GetFruit("苹果");
    fruit->Name();
    fruit = ff->GetFruit("香蕉");
    fruit->Name();

    std::shared_ptr<Factory> factory = FactoryProducer::create("动物");
    std::shared_ptr<Animal> animal = factory->GetAnimal("老虎");
    animal->Name();
    animal = factory->GetAnimal("香蕉");
    animal->Name();
    return 0;
}